package com.github.wxiaoqi.security.biz.modules.merchant.biz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.wxiaoqi.security.biz.common.biz.request.PosLoginReq;
import com.github.wxiaoqi.security.biz.common.cms.request.FsbpCashierCounterQueryParam;
import com.github.wxiaoqi.security.biz.feign.AdminFeign;
import com.github.wxiaoqi.security.biz.feign.CrmFeign;
import com.github.wxiaoqi.security.biz.modules.merchant.entity.FsbpCashierCounter;
import com.github.wxiaoqi.security.biz.modules.merchant.mapper.FsbpCashierCounterMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;


@Slf4j
@Service
public class FsbpCashierCounterBiz extends BaseBiz<FsbpCashierCounterMapper,FsbpCashierCounter>{
	
	@Autowired
	private FsbpCashierCounterMapper fsbpCashierCounterMapper;
	@Autowired
	private AdminFeign adminFeign;
	@Autowired
	private CrmFeign crmFeign;

	
	public List<FsbpCashierCounter> querySeletive(FsbpCashierCounter fsbpCashierCounter) {
		Example example=new Example(FsbpCashierCounter.class);
		Criteria criteria =example.createCriteria();
		if(fsbpCashierCounter.getSeqNo()!=0){
			criteria.andEqualTo("seqNo", fsbpCashierCounter.getSeqNo());
		}
		if(!StringUtils.isEmpty(fsbpCashierCounter.getCounterNo())){
			criteria.andEqualTo("counterNo", fsbpCashierCounter.getCounterNo());
		}
		if(!StringUtils.isEmpty(fsbpCashierCounter.getNickName())){
			criteria.andEqualTo("nickName", fsbpCashierCounter.getNickName());
		}
		if(!StringUtils.isEmpty(fsbpCashierCounter.getPassword())){
			criteria.andEqualTo("password", fsbpCashierCounter.getPassword());
		}
		if(!StringUtils.isEmpty(fsbpCashierCounter.getTerminalType())){
			criteria.andEqualTo("terminalType", fsbpCashierCounter.getTerminalType());
		}
		if(!StringUtils.isEmpty(fsbpCashierCounter.getMerNo())){
			criteria.andEqualTo("merNo", fsbpCashierCounter.getMerNo());
		}
    	return fsbpCashierCounterMapper.selectByExample(example);
    }

	
	public void save(FsbpCashierCounter queryCounterInfoEntity) {
		fsbpCashierCounterMapper.insertSelective(queryCounterInfoEntity);
	}

	
	public void update(FsbpCashierCounter queryCounterInfoEntity){
		fsbpCashierCounterMapper.updateByPrimaryKeySelective(queryCounterInfoEntity);
	}
	
	
	public TableResultResponse<FsbpCashierCounter> findPagables(FsbpCashierCounterQueryParam param) {
		Example example = new Example(FsbpCashierCounter.class);
		Criteria criteria = example.createCriteria();
		if(param.getSeqNo()!=0){
			criteria.andEqualTo("seqNo", param.getSeqNo());
		}
		if(!StringUtils.isEmpty(param.getCounterNo())){
			criteria.andEqualTo("counterNo", param.getCounterNo());
		}
		if(!StringUtils.isEmpty(param.getNickName())){
			criteria.andEqualTo("nickName", param.getNickName());
		}
		if(!StringUtils.isEmpty(param.getPassword())){
			criteria.andEqualTo("password", param.getPassword());
		}
		if(!StringUtils.isEmpty(param.getTerminalType())){
			criteria.andEqualTo("terminalType", param.getTerminalType());
		}
		if(!StringUtils.isEmpty(param.getMerNo())){
			criteria.andEqualTo("merNo", param.getMerNo());
		}
		Page<FsbpCashierCounter> result = PageHelper.startPage(param.getPage(), param.getLimit());
		List<FsbpCashierCounter> list = mapper.selectByExample(example);
		TableResultResponse<FsbpCashierCounter> table = new TableResultResponse<>(result.getTotal(), list);
		return table;
    }
	
	public int deleteBatch(String counterNo){
		return fsbpCashierCounterMapper.deleteBatch(counterNo);
	}
	
	/**
	 * 商户号登陆
	 * @param request
	 * @return
	 */
	public Map<String, Object> queryUserInfo(PosLoginReq request){
		log.info("查询登录信息开始...{}",request);
		String counterNo = request.getCounterNo();
		String password = request.getPassword();
		Map<String, Object> reqMap = new HashMap<String, Object>();
		reqMap.put("merchantId", counterNo);
		reqMap.put("password",password);
		Map<String, Object> userMap = adminFeign.query(reqMap);
		List<Map<String, Object>> userList =  (List<Map<String, Object>>) userMap.get("list");
		if(userList.size() == 0){
			Map<String, Object> errorMap = new HashMap<String, Object>();
			errorMap.put("code", "04");
			errorMap.put("msg", "用户信息不存在");
			return errorMap;
		}
		Map<String, Object> loginUser = userList.get(0);
		String userName = (String) loginUser.get("username");
		String acceptBizNo = (String) loginUser.get("platformId");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("platformId", acceptBizNo);
		params.put("callbackType", "00");//支付
		Map<String, Object> response = crmFeign.queryCallbackUrlInfo(params);
		String signKey = null;
		if("0000".equals(response.get("code"))){
			Map<String, Object> data = (Map<String, Object>)response.get("data");
			if(null != data){
				signKey = (String)data.get("callbackKey");
			}
		}
		Map<String, Object> respMap = new HashMap<String, Object>();
		respMap.put("userName", userName);//商户名
		respMap.put("acceptBizNo", acceptBizNo);
		respMap.put("signKey", signKey);
		respMap.put("merNo", counterNo);//商户号
		log.info("查询登录信息结束...");
		return respMap;
	}

	/**
	 * 终端登录
	 * @param request
	 * @return
	 */
	public Map<String, Object> queryCashierUserInfo(PosLoginReq request){
		log.info("查询登录信息开始...{}",request);
		String counterNo = request.getCounterNo();
		String password = request.getPassword();
		String terminalType = request.getTerminalType();
		if(!StringUtils.hasText(terminalType)){
			terminalType = "01";
		}
		FsbpCashierCounter cashierCounterInfo = new FsbpCashierCounter();
		cashierCounterInfo.setCounterNo(counterNo);
		cashierCounterInfo.setPassword(password);
		FsbpCashierCounter counterInfo = fsbpCashierCounterMapper.query(cashierCounterInfo);
		if(null == counterInfo){
			return null;
		}else{
			//app、pos通用
			if("02".equals(counterInfo.getTerminalType())){
				terminalType = "02";
			}
		}
		
		FsbpCashierCounter cashierCounter = new FsbpCashierCounter();
		cashierCounter.setCounterNo(counterNo);
		cashierCounter.setPassword(password);
		cashierCounter.setTerminalType(terminalType);
		FsbpCashierCounter queryCounter = fsbpCashierCounterMapper.query(cashierCounter);
		if(null == queryCounter){
			return null;
		}
		String loginName = queryCounter.getMerNo();
		String nickName = queryCounter.getNickName();
		
		Map<String, Object> reqMap = new HashMap<String, Object>();
		reqMap.put("merchantId", loginName);
		Map<String, Object> userMap = adminFeign.query(reqMap);
		List<Map<String, Object>> userList =  (List<Map<String, Object>>) userMap.get("list");
		if(userList.size() == 0){
			Map<String, Object> errorMap = new HashMap<String, Object>();
			errorMap.put("code", "04");
			errorMap.put("msg", "用户信息不存在");
			return errorMap;
		}
		Map<String, Object> loginUser = userList.get(0);
		String userName = (String) loginUser.get("username");
		String acceptBizNo = (String) loginUser.get("platformId");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("platformId", acceptBizNo);
		params.put("callbackType", "00");//支付
		Map<String, Object> response = crmFeign.queryCallbackUrlInfo(params);
		String signKey = null;
		if("0000".equals(response.get("code"))){
			Map<String, Object> data = (Map<String, Object>)response.get("data");
			if(null != data){
				signKey = (String)data.get("callbackKey");
			}
		}
		Map<String, Object> respMap = new HashMap<String, Object>();
		respMap.put("merName", userName);//商户名
		respMap.put("acceptBizNo", acceptBizNo);
		respMap.put("signKey", signKey);
		respMap.put("merNo", loginName);//商户号
		respMap.put("nickName", nickName);//终端名
		log.info("查询登录信息结束...{}",respMap);
		return respMap;
	}
}
